
package com.xin.di.uav.po;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.xin.di.uav.operate.po.ServiceAreaPO;
import com.xin.di.uav.user.order.po.OrderPO;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface OrderMapper extends BaseMapper<OrderPO> {

    // 动态字段更新（只更新非空字段）
    @Update({
            "<script>",
            "UPDATE orders ",
            "<set>",
            "    <if test='latitude != null'>latitude = #{latitude},</if>",
            "    <if test='longitude != null'>longitude = #{longitude},</if>",
            "    <if test='detailedAddress != null'>detailed_address = #{detailedAddress},</if>",
            "    <if test='personInCharge != null'>person_in_charge = #{personInCharge},</if>",
            "    <if test='contactNumber != null'>contact_number = #{contactNumber},</if>",
            "    <if test='status != null'>status = #{status},</if>",
            "    updated_at = NOW()",

            "</set>",
            "WHERE id = #{id}",
            "</script>"
    })
    int updateByIdNew(OrderPO orderPO);


    @Update({
            "<script>",
            "update orders set updated_at=now(),status=#{status}" +
                    "<if test='fyOrderNo != null'> ,  `fy_order_no`=#{fyOrderNo} </if>",
                    " where id=#{id}",
            "</script>"
    })
    int updateStatus(OrderPO orderPO);

    @Select({"<script>",
            "select id, order_no, quantity, unit_price, total_amount, merchant_id, service_area, user_id, pay_phone,\n" +
                    "                    status, created_at, confirm_pay_time, updated_at, placement_point_id, express_fee, price_payable,\n" +
                    "                    expire_time, platform_code, receive_name, transaction_id, out_refund_no, refund_id,\n" +
                    "                    estimated_delivery_time \n" +
                    "from orders where id =#{id} ",
            "</script>"})
    OrderPO selectById(String id);


    @Select({"<script>",
            "select count(1) \n" +
                    " from orders where 1=1   " +
                    "    <if test='orderNo != null and orderNo != \"\"  '> AND order_no = #{orderNo} </if>",
            "    <if test='payPhone != null and payPhone != \"\"  '> AND pay_phone = #{payPhone} </if>",
            "</script>"})
    int count(@Param("orderNo") String orderNo, @Param("payPhone") String payPhone);


    @Select({"<script>",
            "select id, order_no,fy_order_no,express_fee, quantity, unit_price, total_amount,\n" +
                    "                    merchant_id,pay_phone, service_area, user_id, status, created_at, updated_at \n" +
                    " from orders where 1=1   " +
                    "    <if test='orderNo != null and orderNo != \"\"  '> AND order_no = #{orderNo} </if>",
            "    <if test='payPhone != null and payPhone != \"\"  '> AND pay_phone = #{payPhone} </if>",
            " order by created_at desc   limit #{offset},#{pageSize} ",
            "</script>"})
    List<OrderPO> selectList(@Param("offset") int offset, @Param("pageSize") int pageSize, @Param("orderNo") String orderNo, @Param("payPhone") String payPhone);

    @Select({"<script>",
            "select id, order_no, fy_order_no, quantity, unit_price, total_amount, merchant_id, service_area, user_id,\n" +
                    "                    pay_phone, status, created_at, confirm_pay_time, updated_at, placement_point_id, express_fee,\n" +
                    "                    price_payable, expire_time, platform_code, receive_name, transaction_id, out_refund_no, refund_id,\n" +
                    "                    estimated_delivery_time from orders where fy_order_no=#{fyOrderNo}",
            "</script>"})
    OrderPO selByFyOrderNo(@Param("fyOrderNo") String fyOrderNo);

    @Select({"<script>",
            "select id, order_no,confirm_pay_time, pick_up_code,estimated_delivery_time,quantity, unit_price, total_amount, merchant_id,\n" +
                    "                    service_area,receive_name, user_id, pay_phone, status, created_at, updated_at, placement_point_id, express_fee,\n" +
                    "                    price_payable, expire_time, platform_code from orders where order_no=#{orderSn} ",
            "</script>"})
    OrderPO selByOrderSn(@Param("orderSn") String orderSn);

}
